angular.module('RDash')
    .controller('MainCtrl', ['app', '$scope', "restService", "$uibModal", '$window', 'commonService', MainCtrl])
    .controller('TPushLinkCreateCtrl', ['$scope', '$uibModalInstance', function ($scope, $uibModalInstance) {
        $scope.pushLink = {
            title: '',
            url: ''
        };
        $scope.create = function () {
            if ($scope.pushLink.title == null || $scope.pushLink.title.length == 0) {
                window.alert("标题不能是空的！");
                return;
            }
            if ($scope.pushLink.url == null || $scope.pushLink.url.length == 0) {
                window.alert("链接不能是空的！");
                return;
            }
            $uibModalInstance.close($scope.pushLink);
        };
        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    }]);

function MainCtrl(app, $scope, restService, $uibModal, $window, commonService) {
    console.log('main-ctrl');
    app.setCurUrl('/main');

    $scope.author = app.getAuthor();
    $scope.online = app.getOnline();

    $scope.statisticsDailyData = {}; // 统计的数据
    $scope.pushLinkRanking = []; // 推送的排行
    $scope.coinRanking = []; // 财富排行
    $scope.notifications = []; // 资讯列表
    $scope.notification = null; // 当前资讯

    $scope.statisticsDaily = function () {
        restService.api.statistics.save({ID: "daily"}, null, function (result) {
            if (app.catchResultCode(result['resultCode'])) {
                $scope.statisticsDailyData = result.data;
            }
        })
    };

    $scope.createPushLink = function () {
        if ($scope.author == null || !$scope.online) {
            commonService.toast.error('请先登录!');
            return;
        }

        var modalInstance = $uibModal.open({
            templateUrl: 'templates/template_push_link_create.html',
            controller: 'TPushLinkCreateCtrl',
            backdrop: 'static',
            resolve: {}
        });

        modalInstance.result.then(function (pushLink) {
            restService.api.pushLink.save({
                ID: 'createPushLink'
            }, {
                title: pushLink.title,
                url: pushLink.url
            }, function (result) {
                if (app.catchResultCode(result['resultCode'])) {
                    $window.location.reload();
                }
            });
        }, function () {

        });
    };

    $scope.sortPushLink = function () {
        restService.api.ranking.save({ID: "sortPushLink"}, null, function (result) {
            if (app.catchResultCode(result['resultCode'])) {
                $scope.pushLinkRanking = result.data;
            }
        })
    };

    $scope.sortCoin = function () {
        restService.api.ranking.save({
            ID: "sortCoin"
        }, null, function (result) {
            if (result['resultCode'] == 0) {
                $scope.coinRanking = result.data;
            }
        })
    };

    function listNotifications() {
        restService.api.notification.save({
            ID: 'listNotifications'
        }, {
            page: 1,
            pageSize: 10
        }, function (result) {
            if (app.catchResultCode(result['resultCode'])) {
                $scope.notifications = result['data'];
                if ($scope.notifications.length != 0) {
                    $scope.notification = $scope.notifications[0];
                }
            }
        });
    }

    $scope.statisticsDaily();
    $scope.sortPushLink();
    $scope.sortCoin();

    listNotifications();

    $scope.clickPushLink = function (pushLink) {
        window.open(pushLink.url);
    };

    $scope.getTime = function (time) {
        return new Date(time);
    };

    $scope.setNotification = function (notification) {
        $scope.notification = notification;
    };
}